package com.shine.demo.leetCode.leetCodeOld;

import com.shine.demo.leetCode.entity.ListNode;

/**
 * 给定一个已排序的链表的头 head ， 删除所有重复的元素，使每个元素只出现一次 。返回 已排序的链表 。
 * 链表中节点数目在范围 [0, 300] 内
 * -100 <= Node.val <= 100
 * 题目数据保证链表已经按升序 排列
 */
public class DeleteDuplicates {

    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(2);
        ListNode l4 = new ListNode(3);
        ListNode l5 = new ListNode(3);
        ListNode l6 = new ListNode(4);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;
        l5.next = l6;
        ListNode merged = deleteDuplicates(l1);
        ListNode a = merged;
        while (null != a.next){
            System.out.println(a.val);
            a = a.next;
        }
        System.out.println(a.val);

    }


    public static ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode when = head;
        while (null != when.next) {
            if(when.next.val> when.val){
                when = when.next;
                continue;
            }
            when.next = when.next.next;
        }
        return head;
    }
}
